import CityModel from "../../../models/City";
const cityModel = new CityModel()

Page({
  data: {
    nickname: '',
    mobile: '',
    info: '',
    isDefault: true,
    // areaIndex: [0, 0, -1],
    areaIndex: [0, 0, 0],
    tree: [],
    id: -1,
    city: {}
  },
  onLoad: function (options) {

    // get tree
    cityModel.getTree().then(tree => {
      this.setData({ tree })

      // edit
      if (options.id) {
        cityModel.view({ id: options.id }).then(response => {
          const { data } = response

          // console.log(data)

          const isDefault = data.is_default === '1'

          const index1 = this.data.tree.findIndex(x => {
            return data.city.province_id === x.value
          })
          const index2 = this.data.tree[index1].children.findIndex(x => {
            return data.city.city_id === x.value
          })
          const index3 = this.data.tree[index1].children[index2].children.findIndex(x => {
            return data.city.area_id === x.value
          })
          // console.log(index1, index2, index3)

          this.setData({
            isDefault,
            id: options.id,
            nickname: data.nickname,
            mobile: data.mobile,
            info: data.address,
            areaIndex: [index1, index2, index3],
            city: data.city
          })
        })
      }

    })

    this.setData({ type: +options.type })
  },
  bindNickname(e) {
    const value = e.detail.value
    this.setData({ nickname: value })
  },
  bindMobile(e) {
    const value = e.detail.value
    this.setData({ mobile: value })
  },
  BindDefault(e) {
    const value = e.detail.value[0]
    if (value === 'default') {
      this.setData({ isDefault: true })
    } else {
      this.setData({ isDefault: false })
    }
  },
  bindInfo(e) {
    const value = e.detail.value
    this.setData({ info: value })
  },

  saveCity(e) {
    const value = e.detail.city
    this.setData({ areaIndex: value })
  },

  save() {
    const data = this.data
    if (!data.nickname) {
      return wx.showToast({ title: '请输入收件人姓名', icon: 'error' })
    }
    if (!data.mobile) {
      return wx.showToast({ title: '请输入收件人电话', icon: 'error' })
    }
    if (!data.info) {
      return wx.showToast({ title: '请输入详情', icon: 'error' })
    }

    // save
    const index = this.data.areaIndex
    const tree = this.data.tree
    // console.log(index)
    // console.log(tree)
    cityModel.edit({
      id: this.data.id,
      province_id: tree[index[0]].value,
      city_id: tree[index[0]].children[index[1]].value,
      // area_id: index[2] > -1 ? tree[index[0]].children[index[1]].children[index[2]].value : -1,
      area_id: tree[index[0]].children[index[1]].children
        && tree[index[0]].children[index[1]].children.length > 0
        // && tree[index[0]].children[index[1]].children[index[2]].value
        // && tree[index[0]].children[index[1]].children[index[2]].value > 0
        ? tree[index[0]].children[index[1]].children[index[2]].value : -1,
      is_default: this.data.isDefault ? 1 : 0,
      mobile: this.data.mobile,
      nickname: this.data.nickname,
      address: this.data.info
    }).then(() => {
      if (this.data.type === -2) {
        wx.navigateBack()
      } else {
        wx.redirectTo({
          url: '/sub_service/pages/address-list/index'
        })
        // wx.navigateBack()
      }
    })
  }
});
